package com.tys.algorithm.beginner;

public class Code5_Fsort {

    //选择排序
    public static void selectSort(int[] arr) {
        //判断边界条件
        if (arr == null || arr.length < 2) {
            return;
        }

        //0~n-1
        //1~n-1
        //2~n-1
        //i~n-1
        int N = arr.length;
        for (int i = 0; i < N; i++) {
            int minValueIndex = i;
            for (int j = i + 1; j < N; j++) {
                minValueIndex = arr[j] < arr[minValueIndex] ? j : minValueIndex;
            }
            swap(arr, minValueIndex, i);
        }
    }

    //交换
    private static void swap(int[] arr, int minValueIndex, int i) {
        int temp = arr[i];
        arr[i] = arr[minValueIndex];
        arr[minValueIndex] = temp;
    }

    //打印
    public static void printArray(int[] arr) {
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + " ");
        }
        System.out.println();
    }


    //冒泡排序
    public static void bubbleSort(int[] arr) {
        if (arr == null || arr.length < 2) {
            return;
        }

        int N = arr.length;
        for (int end = N - 1; end >= 0; end--) {
            for (int second = 1; second <= end; second++) {
                if (arr[second - 1] > arr[second]) {
                    swap(arr, second - 1, second);
                }
            }
        }


    }


    //插入排序
    public static void insertSort1(int[] arr) {
        if (arr == null || arr.length < 2) {
            return;
        }

        int N = arr.length;
        for (int end = 1; end < N; end++) {
            int newNumIndex = end;
            while (newNumIndex - 1 >= 0 && arr[newNumIndex - 1] > arr[newNumIndex]) {
                swap(arr, newNumIndex - 1, newNumIndex);
                newNumIndex--;
            }
        }

    }

    //插入排序-优化
    public static void insertSort2(int[] arr) {
        if (arr == null || arr.length < 2) {
            return;
        }

        int N = arr.length;
        for (int end = 1; end < N; end++) {
            for (int pre = end - 1; pre >= 0 && arr[pre] > arr[pre + 1]; pre--) {
                swap(arr, pre, pre + 1);
            }
        }

    }

    //获取长度随机，值随机的数组
    public static int[] lenRandomValueRandom(int maxLen, int maxValue) {
        int len = (int) (Math.random() * maxLen);
        int[] ans = new int[len];
        for (int i = 0; i < len; i++) {
            ans[i] = (int) (Math.random() * maxValue);
        }
        return ans;
    }

    //拷贝数组
    public static int[] copyArray(int[] arr) {
        int[] ans = new int[arr.length];
        for (int i = 0; i < arr.length; i++) {
            ans[i] = arr[i];
        }
        return ans;
    }

    //判断arr1和arr2是否完全相等
    public static boolean equalValues(int[] arr) {
        if (arr == null || arr.length < 2) {
            return true;
        }
        int max = arr[0];

        for (int i = 1; i < arr.length; i++) {
            if (max > arr[i]) {
                return false;
            }
        }
        return true;
    }

    public static void main(String[] args) {

        int maxLen = 10;
        int maxValue = 10;
        int testTimes = 10;

        for (int i = 0; i < testTimes; i++) {
            int[] arr1 = lenRandomValueRandom(maxLen, maxValue);
            printArray(arr1);
            bubbleSort(arr1);
            printArray(arr1);

            int[] tmp = copyArray(arr1);
            if (!equalValues(arr1)) {
                printArray(tmp);
                System.out.println("排序错误");
                break;
            }
        }


    }
}
